// pages/address/address.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts: [
    ],
  },
  switchSelect: function (e) {
    let that =this;
    var i = 0;
    var index = e.target.dataset.index;
    var list = that.data.carts;
    for (var i = 0; i < list.length;i++){
      console.log(list[i].default)
      list[i].default=0
    }
    list[index].default = 1
    that.setData({
      carts:list
    })
    
    wx.request({
      url: app.data.Url +'address/defaultsite',
      data:{
        uaid: e.target.dataset.id,
        uid:wx.getStorageSync('userInfo').uid
      },
      success:function(res){ 
        if(res.data==1){
          wx.showToast({
            title: '设置成功',
          })
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  get_address(){
      let that = this
      wx.request({
          url: app.data.Url + 'address/addressList',
          data: {
              uid: wx.getStorageSync('userInfo').uid
          },
          method: 'get',
          success: function (res) {
              that.setData({
                  carts: res.data.data
              })
              console.log(res.data.data)
          }
      })
  },

  //选择
  choice:function(e){
    wx.setStorage({
      key: "address",
      data: this.data.carts[e.currentTarget.dataset.index],
    })
    wx.navigateBack({
      delta: 1
    })
  },

  del: function (e) {
      var that = this;
      wx.request({
          url: app.data.Url + 'address/deladdress',
          data: {
              uaid: e.currentTarget.id
          },
          method: 'get',
          success: function (res) {
              if (res.data == 1) {
                  wx.showToast({
                      title: '删除成功',
                  })
                  that.get_address()
              }
          }
      })
  },
  save: function (e) {
    let that = this;
    var carts = that.data.carts;
    wx.setStorage({
        key:'edit_address_info',
      data: carts[e.currentTarget.dataset.index],
        success(){
            wx.navigateTo({
                url: '../saveAddress/addAddress'
            })
        }
    })
      
  },
  onLoad: function (options) {
      wx.showToast({
          icon:'loading'
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_address()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  
})